<?php
$str=file_get_contents('http://www.17k.com/');
//echo $str;
$preg='#男生订阅周榜.*姽婳晴雨新书：农园医锦#isU';
preg_match($preg,$str,$arr);
//print_r($arr);die;
$preg_match='#  <ul class=".*"><li> <span class=".*">.*</span> <a href="(.*)" title=".*" target="_blank">(.*)</a></li>.*</ul>#isU';
preg_match_all($preg_match,$arr[0],$arr_gg);
//print_r($arr_gg);
//foreach($arr_gg[1] as $key=>$value) {
//    $arr_cc['novel_url'] = $arr_gg[1][$key];
//    $arr_cc['novel_title'] = $arr_gg[2][$key];
//    $dbh=new PDO('mysql:host=localhost;dbname=gaofei','root','root');
//    $sql="insert into novel(novel_url,novel_title) VALUES('{$arr_cc['novel_url']}','{$arr_cc['novel_title']}')";
//    $dbh->exec($sql);
//
//}
$dbh=new PDO('mysql:host=localhost;dbname=gaofei','root','root');
//预处理一条语句
$pre=$dbh->prepare("INSERT INTO novel (novel_url,novel_title) VALUES (?,?)");
$pre->bindParam(1,$novel_url);
$pre->bindParam(2,$novel_title);
//开启一个事务
$dbh->beginTransaction();

foreach($arr_gg[1] as $k=>$v)
{
    $novel_url=$v;
    $novel_title=$arr_gg[2][$k];
    $query=$pre->execute();
    if($query==true)
    {
        $dbh->commit();
    }
    else{
        $dbh->rollBack();
    }
}
